﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFCons
{
	public partial class CoordinateControlButton : UserControl
	{
		public EventHandler<bool> TeachClicked;
		public CoordinateControlButton()
		{
			InitializeComponent();
			tbes.Add(textBoxX1); tbes.Add(textBoxY1); tbes.Add(textBoxU1); tbes.Add(textBoxV1);
			tbes.Add(textBoxX2); tbes.Add(textBoxY2); tbes.Add(textBoxU2); tbes.Add(textBoxV2);
			tbes.Add(textBoxX3); tbes.Add(textBoxY3); tbes.Add(textBoxU3); tbes.Add(textBoxV3);
			tbes.Add(textBoxX4); tbes.Add(textBoxY4); tbes.Add(textBoxU4); tbes.Add(textBoxV4);
			textBoxX1.Tag = "F2"; textBoxY1.Tag = "F2"; textBoxU1.Tag = "F0"; textBoxV1.Tag = "F0";
			textBoxX2.Tag = "F2"; textBoxY2.Tag = "F2"; textBoxU2.Tag = "F0"; textBoxV2.Tag = "F0";
			textBoxX3.Tag = "F2"; textBoxY3.Tag = "F2"; textBoxU3.Tag = "F0"; textBoxV3.Tag = "F0";
			textBoxX4.Tag = "F2"; textBoxY4.Tag = "F2"; textBoxU4.Tag = "F0"; textBoxV4.Tag = "F0";

			buttonTeachXY1.Click += ButtonTeachXY_Click;
			buttonTeachXY2.Click += ButtonTeachXY_Click;
			buttonTeachXY3.Click += ButtonTeachXY_Click;
			buttonTeachXY4.Click += ButtonTeachXY_Click;
			buttonTeachXY1.Tag = 0;
			buttonTeachXY2.Tag = 1;
			buttonTeachXY3.Tag = 2;
			buttonTeachXY4.Tag = 3;

			buttonTeachUV1.Click += ButtonTeachUV_Click;
			buttonTeachUV2.Click += ButtonTeachUV_Click;
			buttonTeachUV3.Click += ButtonTeachUV_Click;
			buttonTeachUV4.Click += ButtonTeachUV_Click;
			buttonTeachUV1.Tag = 0;
			buttonTeachUV2.Tag = 1;
			buttonTeachUV3.Tag = 2;
			buttonTeachUV4.Tag = 3;

		}

		private void ButtonTeachXY_Click(object sender, EventArgs e)
		{
			if (TeachClicked != null) TeachClicked(sender, true);
		}
		private void ButtonTeachUV_Click(object sender, EventArgs e)
		{
			if (TeachClicked != null) TeachClicked(sender, false);
		}

		List<TextBox> tbes = new List<TextBox>();

		public void Data2UI(List<float> list)
		{
			try
			{
				for (int i = 0; i < list.Count && i < tbes.Count; i++)
				{
					tbes[i].Text = list[i].ToString((string)tbes[i].Tag);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, "ERROR");
			}
		}

		public bool UI2Data(List<float> calib_xyuv)
		{
			try
			{
				foreach (var btn in tbes)
				{
					float fv = Convert.ToSingle(btn.Text);
					calib_xyuv.Add(fv);
				}

				return true;
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, "Coordinate.UI2Data" + Environment.NewLine + ex.Message, "ERROR");
				return false;
			}
		}

		public void SetXY(int index, float x, float y)
		{
			if (index == 0)
			{
				textBoxX1.Text = x.ToString((string)textBoxX1.Tag);
				textBoxY1.Text = y.ToString((string)textBoxY1.Tag);
			}
			else if (index == 1)
			{
				textBoxX2.Text = x.ToString((string)textBoxX2.Tag);
				textBoxY2.Text = y.ToString((string)textBoxY2.Tag);
			}
			else if (index == 2)
			{
				textBoxX3.Text = x.ToString((string)textBoxX3.Tag);
				textBoxY3.Text = y.ToString((string)textBoxY3.Tag);
			}
			else if (index == 3)
			{
				textBoxX4.Text = x.ToString((string)textBoxX4.Tag);
				textBoxY4.Text = y.ToString((string)textBoxY4.Tag);
			}

		}

		public void SetUV(int index, float u, float v)
		{
			if (index == 0)
			{
				textBoxU1.Text = u.ToString((string)textBoxU1.Tag);
				textBoxV1.Text = v.ToString((string)textBoxV1.Tag);
			}
			else if (index == 1)
			{
				textBoxU2.Text = u.ToString((string)textBoxU2.Tag);
				textBoxV2.Text = v.ToString((string)textBoxV2.Tag);
			}
			else if (index == 2)
			{
				textBoxU3.Text = u.ToString((string)textBoxU3.Tag);
				textBoxV3.Text = v.ToString((string)textBoxV3.Tag);
			}
			else if (index == 3)
			{
				textBoxU4.Text = u.ToString((string)textBoxU4.Tag);
				textBoxV4.Text = v.ToString((string)textBoxV4.Tag);
			}
		}
	}
}
